package com.shengyuan.manage_os.old.ying.erpm;

import org.json.JSONException;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.im.ECApplication;
import com.shengyuan.manage_os.im.common.utils.ToastUtil;
import com.shengyuan.manage_os.old.ying.erpm.controls.CurrentPersonal;
import com.shengyuan.manage_os.old.ying.erpm.controls.CurrentTime;
import com.shengyuan.manage_os.old.ying.erpm.controls.GpsButton;
import com.shengyuan.manage_os.old.ying.erpm.controls.PictureButton;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.app.PendingIntent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import static com.baidu.location.LocationClientOption.LocationMode.Hight_Accuracy;

public class Actual_OA_DaKa extends BaseActivity  implements
		OnGetGeoCoderResultListener {

	LocationClient mLocClient;
	public MyLocationListenner myListener = new MyLocationListenner();

	public static String UserName;
	public static String DengJi, Type="";
	protected PictureButton PictureButton_myself;
	private BaiduMap aMap;
	private MapView mapView;
	public static PendingIntent pi = null;
	boolean isFirstLoc = true;
	GeoCoder mSearch = null;
	private View viewCache;
	private TextView tvResult;
	MapView mMapView;
	BaiduMap mBaiduMap;

	@Override
	public void onClick(View view) {
		super.onClick(view);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actual_oa_daka);
		InitControls();

		if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS)) {
			ToastUtil.showMessage("您的手机没有GPS硬件");
			finish();
		}

		mapView = (MapView) findViewById(R.id.map);
		aMap = mapView.getMap();
		aMap.setMyLocationEnabled(true);
		mSearch = GeoCoder.newInstance();
		mSearch.setOnGetGeoCodeResultListener(this);
		setUpMap();
		System.out.print("&&&&&&&&&&Actual_OA_DaKa");
		
	}
	private void setUpMap() {

		viewCache = LayoutInflater.from(this)
				.inflate(R.layout.pop_layout, null);
		tvResult = (TextView) viewCache.findViewById(R.id.location_tips);
		mMapView = (MapView) findViewById(R.id.map);
		mBaiduMap = mMapView.getMap();
		mBaiduMap.setMyLocationEnabled(true);
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setCoorType("bd09ll");
//		option.setAddrType("all");
		option.setIsNeedAddress(true);
		option.setIsNeedLocationDescribe(true);
		option.setLocationMode(Hight_Accuracy);
		option.setScanSpan(15*60*1000);
		mLocClient.setLocOption(option);
		mLocClient.start();
	}

	private String address = "";
	private BDLocation bdLocation;
	public class MyLocationListenner implements BDLocationListener {

		private InfoWindow infoWindow;

		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null || mapView == null) {
				return;
			}
			bdLocation = location;
			location.setRadius(10.0f);
			MyLocationData locData = new MyLocationData.Builder()
					.accuracy(location.getRadius())
					.direction(100).latitude(location.getLatitude())
					.longitude(location.getLongitude()).build();
			LatLng ll = null;
			address = location.getAddrStr()+"("+location.getLocationDescribe()+")";
			aMap.setMyLocationData(locData);
//			if (isFirstLoc) {
//				isFirstLoc = false;

				location.getLongitude();
				ll = new LatLng(location.getLatitude(), location.getLongitude());
				float f = aMap.getMaxZoomLevel();//19.0
				MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, f-8);
				aMap.animateMapStatus(u);
//			}

			if (ll != null) {
				mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(ll));
			}
		}

		public void onReceivePoi(BDLocation poiLocation) {
		}
	}

	@Override
	public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

	}

	@Override
	public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
		// TODO Auto-generated method stub

		if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
			return;
		}

		if(aMap==null){
			return;
		}

		tvResult.setText(address);
//		address=result.getAddress();
		InfoWindow infoWindow = new InfoWindow(viewCache, result.getLocation(),
				-90);
		mBaiduMap.showInfoWindow(infoWindow);
		System.out.println("FFFFFFFFFF@GeoCodeResult===="+address);
		MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(result.getLocation());


		mBaiduMap.animateMapStatus(u);

		mBaiduMap.addOverlay(new MarkerOptions()
				.position(result.getLocation())
				.title(result.getAddress())
				.icon(com.baidu.mapapi.map.BitmapDescriptorFactory
						.fromResource(R.drawable.icon_marka)));

	}
	@Override
	protected void SaveJsonData() {
		try {
			IsJson1 = true; mFinished = 1; mPrintenabled= 0;
			if (Type.equals("JIDONG")) {
				if (DengJi.equals("DengJiTime1")) {
					mPrintcount = 1;
					//SetAlarmActivity();
				}else {
					mPrintcount = 2;
					if (pi!=null) {
						MainApplication.mAlarmManager.cancel(pi);
					}
				}
			}
			JsonMyID();
			getDataExt();

			mJson.put( "Field",  DengJi );
			mJson.put( "Type", Type);
			mJson.put( getString(R.string.title_actual_form_editText_personal),  LoginActivity.pmUser.UserName);
			mJson.put( getString(R.string.title_actual_form_currenttime),  CurrentTime.getLongTime() );
			getMemo();
			JsonPicture_Ext();
		}catch (Exception e) {
		}
	}
	
	protected void getDataExt() throws JSONException {
		double x = bdLocation.getLatitude() ;
		double y = bdLocation.getLongitude();
		String s = address;
		mJson.put( getString(R.string.title_actual_form_GpsButton_Gps), s + "," + x + "," + y);
	}
	
	protected void JsonPicture_Ext() throws JSONException {
		if (PictureButton_myself!=null) {
			mJson.put( getString(R.string.title_actual_form_myself), PictureButton_myself.mFileName);
		}
	}	
	@Override
	protected void SaveSelfPic(String sPath) {
		try {
			CopyPicDest(PictureButton_myself, sPath);
		} catch (Exception e) {
			Utility.MessageBox(this,e.getMessage());
		}
	}

	@Override
	public void Back() {
		CurrentPersonal.UserName ="";
		super.Back();
	}

	@Override
	public void InitControls() {
		super.InitControls();
		PictureButton_myself = (PictureButton) findViewById(R.id.PictureButton_myself);
	}
	
	@Override
	public Boolean IsPassed() {
		if (super.IsPassed()) {
			if (!PictureButton_myself.IsExist()) {
				Utility.MessageBox(this, PictureButton_myself.FIELD );
				return false;
			}
			if ("".equals(address) || address == null ) {
				Utility.MessageBox( this,"没有定位信息，请等待一会儿！" );
				return false;
			}
			else
				return true;
		}else {
			return false;
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		PictureButton_myself.postInvalidate();
		mapView.onResume();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onPause() {
		super.onPause();
		mapView.onPause();
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mapView.onSaveInstanceState(outState);
	}

	/**
	 * 方法必须重写
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mapView.onDestroy();
	}



}
